<?php require_once("../includes/initializeSession.php"); ?>
<?php
if (!isset($_SESSION['admin'])) {
    header('Location: adminLogin.php');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       <?php require_once("../includes/cssAndJsAdmin.php"); ?>
                    <script type="text/javascript">

                        $(document).ready(function() {
                            $("#agentList").jqGrid({
                                url: "../gridFunctions/agentActions.php",
                                editurl: "../gridFunctions/agentActions.php",
                                direction: 'rtl',
                                async: false,
                                datatype: 'json',
                                pager: '#pager',
                                colNames: ['מספר סוכן', 'ת"ז', 'שם פרטי', 'שם משפחה', 'מייל', 'עיר', 'רחוב ובית', 'טלפון', 'פקס', 'פלאפון', 'סיסמא', 'חברה', 'מחירון', 'סוכן מטריה', 'סוכן בוס', 'ע.ס.מ'],
                                colModel: [
                                    {name: 'person_number', index: 'person_number', editable: true, width: 60, editoptions: {readonly: "readonly"}, align: 'right'},
                                    {name: 'person_id', index: 'person_id', width: 60, editable: true, align: 'right', editrules: {required: true, integer: true}},
                                    {name: 'firstname', index: 'firstname', width: 120, editable: true, align: 'right', editrules: {required: true}},
                                    {name: 'lastname', index: 'lastname', width: 120, editable: true, align: 'right', editrules: {required: true}},
                                    {name: 'email', index: 'email', width: 120, editable: true, align: 'right', editrules: {required: true, email: true}},
                                    {name: 'city', index: 'city', width: 70, editable: true, align: 'right', editrules: {required: true}},
                                    {name: 'address', index: 'address', width: 70, editable: true, align: 'right', editrules: {required: true}},
                                    {name: 'telephone', index: 'telephone', width: 80, editable: true, align: 'right', editrules: {custom: true, custom_func: phoneCalidtion}},
                                    {name: 'fax', index: 'fax', width: 60, editable: true, align: 'right', editrules: {custom: true, custom_func: phoneCalidtion}},
                                    {name: 'cellphone', index: 'cellphone', width: 180, editable: true, align: 'right', editrules: {custom: true, custom_func: phoneCalidtion}},
                                    {name: 'pass_word', index: 'pass_word', width: 70, editable: true, align: 'right', editrules: {required: true}},
                                    {name: 'companyname', index: 'companyname', width: 70, editable: true, align: 'right', editrules: {required: true}},
                                    {name: 'mehiron_number', index: 'mehiron_number', width: 80, align: 'right'},
                                    {name: 'metria', index: 'metria', width: 60, editable: true, edittype: 'checkbox', editoptions: {value: "1:0"},formatter: "checkbox", align: 'right'},
                                    {name: 'underagent', index: 'underagent', width: 180, editable: true, edittype: "select", editoptions: {}, align: 'right'},
                                 {name: 'metriaAmla', index: 'metriaAmla', width: 80, editable: true, align: 'right'}],
                                loadComplete: function() {
                                    $.ajax({url: '../gridFunctions/agentActions.php',
                                        data: {oper: 'getMetriaAgent'},
                                        type: 'post',
                                        success: function(output)
                                        {
                                            output = output.substr(0, output.length - 1);
                                            output = output.substr(1);
                                            output = output.replace(/{/g, "");
                                            output = output.replace(/}/g, "");
                                            output = '{"0":"עצמאי",' + output + "}";
                                            console.log(output);
                                            var catagentList = eval('(' + output + ')');
                                            jQuery("#agentList").setColProp('underagent', {editoptions: {value: catagentList}})
                                        }
                                    });	//end og ajax call
                                },
                                pager: jQuery('#pager'),
                                        rowNum: 20,
                                rowagentList: [5, 10, 20, 50],
                                sortname: 'person_number',
                                sortorder: "desc",
                                mtype: "POST",
                                ajaxSelectOptions: {cache: false},
                                //loadonce: true,
                                postData: {oper: 'view'},
                                viewrecords: true,
                                autowidth: true,
                                height: "100%"
                            }); // -- end of   $("#agentList").jqGrid({

                            $("#agentList").jqGrid('navGrid', '#pager', {edit: true, add: true, del: true, refresh: true, closeAfterAdd: true, reloadAfterSubmit: true, search: false},
                            // Edit dialog parameters
                            {
                                reloadAfterSubmit: true,
                                closeAfterEdit: true,
                                //editData: {person_number: function() {  return "7"; } },
                                /*onclickSubmit: function (rp_ge, postdata) 
                                 {
                                 return { person_number: getSelectedCell('person_number') };
                                 },*/
                                afterSubmit: function(response, postdata)
                                {
                                    if (response.responseText == "okok")
                                    {
                                        success = true;
                                        alert("פרטי הסוכן התעדכנו בהצלחה")
                                    }
                                    else
                                    {

                                        success = false;
                                        alert("פרטי הסוכן לא התעדכנו בהצלחה")
                                    }
                                    return [success, response.responseText]
                                }
                            },
                            // Add dialog parameters
                            {
                                /*	onclickSubmit: function (rp_ge, postdata) 
                                 {
                                 return { person_number: getSelectedCell('person_number') };
                                 },*/
                                recreateForm: true,
                                reloadAfterSubmit: true,
                                closeAfterAdd: true,
                                afterSubmit: function(response, postdata) {
                                    if (response.responseText == "okok")
                                    {
                                        success = true;
                                        alert("סוכן התווסף בהצלחה");
                                    }
                                    else
                                    {
                                        success = false;
                                        alert("סוכן לא התווסף בהצלחה");
                                    }
                                    return [success, response.responseText];
                                }

                            },
                            // Delete dialog parameters
                            {
                                reloadAfterSubmit: true,
                                onclickSubmit: function(rp_ge, postdata)
                                {
                                    return {person_number: getSelectedCell('person_number')};
                                },
                                afterSubmit: function(response, postdata) {
                                    if (response.responseText == "okok")
                                    {
                                        success = true;
                                        alert("סוכן נמחק בהצלחה")
                                    }
                                    else
                                    {
                                        success = false;
                                        alert("סוכן לא נמחק בהצלחה")
                                    }
                                    return [success, response.responseText]
                                }

                            },
                            // Search dialog parameters
                            {},
                                    // View dialog parameters
                                            {}
                                    );

                                    $('#agentList').jqGrid('filterToolbar', {searchOnEnter: true, enableClear: false});



                                });// -- end of  $(document).ready(function ()
                        function getSelectedCell(str)
                        {
                            var grid = jQuery('#agentList');
                            var sel_id = grid.jqGrid('getGridParam', 'selrow');
                            var myCellData = grid.jqGrid('getCell', sel_id, str);
                            return myCellData;
                        }



                    </script>
                    <title>ניהול סוכנים</title>
                    </head>
                    <body dir="rtl"  style="padding:0px; margin:0px;">
                        <div id="main">
                            <?php include("adminHeader.php"); ?>
                            <h1>רשימת סוכנים</h1>
                            <div id="lc">
                                <table id="agentList" class="scroll" cellpadding="0" cellspacing="0">
                                </table>
                                <div id="pager" class="scroll" style="text-align: center;"> </div>
                            </div>
                            <?php include("../footer.php"); ?>	
                        </div>
                    </body>
                    </html>